package com.mediacloud.app.newsmodule.activity

import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.mediacloud.app.assembly.util.load
import com.mediacloud.app.model.activity.BaseBackActivity
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.news.CatalogItem
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.adaptor.NewsListItemStyleAdaptor
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils
import com.mediacloud.app.reslib.util.DataInvokeUtil
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_curation_list.*
import org.json.JSONObject


/**
 * 精选榜单列表
 */
class CurationListActivity: BaseBackActivity(), OnRefreshLoadMoreListener {
    var pageNum = 1
    var listType : String = "1"
    var newsListItemStyleAdaptor:NewsListItemStyleAdaptor?=null
    var listData = mutableListOf<ArticleItem>()

    override fun getLayoutResID(): Int {
        return R.layout.activity_curation_list
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val title = intent.getStringExtra("title")
        val headImg = intent.getStringExtra("head_img")
        listType = intent.getStringExtra("list_type")!!
        setTitle(title)
        mTitileBar.setBackgroundColor( Color.WHITE)
        showStateView(TYPE_LOADING,false)
        if(TextUtils.isEmpty(headImg)){
            ivHeadImg.visibility = View.GONE
        }else{
            ivHeadImg.load(headImg)
            ivHeadImg.visibility = View.VISIBLE
            closeStateView()
        }
        newsListItemStyleAdaptor = NewsListItemStyleAdaptor(this,CatalogItem())
        newsListItemStyleAdaptor?.listView = rvList
        rvList.adapter = newsListItemStyleAdaptor
        rvList.setOnItemClickListener { parent, view, position, id ->
            newsListItemStyleAdaptor?.let{
                val item = it.listContentData[position]
                NewsItemClickUtils.OpenItemNewsHandle(view.context, item.type, item, CatalogItem())
            }
        }
        fresh.setOnRefreshLoadMoreListener(this)
        loadingView = findViewById(R.id.mLoadingView)
        initStateView()
        loadingImage.setOnClickListener {
            if (currentState != TYPE_LOADING) {
                showStateView(TYPE_LOADING, false)
                pageNum = 1
                getList()
            }
        }
        getList()
    }


    private fun getList() {
        DataInvokeUtil.getZiMeiTiApi().getChoiceList(pageNum,10)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<JSONObject>{
                override fun onSubscribe(d: Disposable) {
                }

                override fun onNext(t: JSONObject) {
                    if (isDestroyed || isFinish) return
                    t.let {
                        val data = it.optJSONObject("data")
                        val state = it.optBoolean("state")
                        if (state) {
                            val optJSONArray = data.optJSONArray("meta")
                            for (index in 0 until optJSONArray.length()) {
                                val item = optJSONArray.optJSONObject(index)
                                val parse = ArticleItem.parse(item)
                                listData.add(parse)
                            }
                            if (listData.size > 0){
                                closeStateView()
                                if (pageNum == 1){
                                     newsListItemStyleAdaptor?.listContentData = listData
                                }else{
                                     newsListItemStyleAdaptor?.listContentData?.addAll(listData)
                                }
                                newsListItemStyleAdaptor?.notifyDataSetChanged()
                            }else{
                                if (ivHeadImg.visibility != View.VISIBLE)
                                 showStateView(TYPE_NO_CONTENT, false)
                                else closeStateView()
                            }
                        }else{
                            if (ivHeadImg.visibility != View.VISIBLE)
                                showStateView(TYPE_NET_NOT_GIVE_FORCE, false)
                            else closeStateView()
                        }
                    }
                }

                override fun onError(e: Throwable) {
                    if (ivHeadImg.visibility != View.VISIBLE)
                        showStateView(TYPE_NET_NOT_GIVE_FORCE, false)
                    else closeStateView()
                }

                override fun onComplete() {

                }

            })
    }
    override fun getContent_show_top_color(): String? {
        return "#333333"
    }


    override fun getStatusBarColor(): Int {
        return Color.WHITE
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        pageNum = 1
        getList()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        pageNum ++
        getList()
    }

    override fun onDestroy() {
        super.onDestroy()
        newsListItemStyleAdaptor?.destroy()
    }
}